<?php
/**
 * 添加职位
 *
 * @author Dracowyn
 * @since 2023-11-17 17:55
 */

// 引入配置文件
include_once('config/config.php');
$loginAuth = check_auth();

$depSql = "SELECT * FROM pre_department";
$depData = all($depSql);

if ($_POST) {
	$name = trim($_POST['name']);
	$depId = trim($_POST['depId']);
	if (empty($name)) {
		showMsg('职位名称不能为空');
		exit();
	}
	if (empty($depId)) {
		showMsg('请选择部门');
		exit();
	}
	$sql = "SELECT name FROM pre_job WHERE name = '$name' AND depid = '$depId'";
	$findName = find($sql);
	if ($findName) {
		showMsg('职位名称已存在');
		exit();
	}
	$data = [
		'name' => $name,
		'depid'=>$depId,
	];
	$res = insert('job', $data);
	if ($res['code'] === 0) {
		showMsg($res['msg']);
	} else {
		showMsg('添加成功', 'jobList.php');
	}
}

?>

<!DOCTYPE html>
<html lang="zh">
<head>
	<?php include_once('common/meta.php'); ?>
	<title>添加职位</title>
</head>

<body>

<?php include_once('common/header.php'); ?>

<?php include_once('common/menu.php'); ?>

<div class="content">
	<div class="header">
		<h1 class="page-title">添加职位</h1>
	</div>
	<ul class="breadcrumb">
		<li><a href="index.php">首页</a> <span class="divider">/</span></li>
		<li><a href="adminList.php">职位管理</a> <span class="divider">/</span></li>
		<li class="active">添加职位</li>
	</ul>

	<div class="container-fluid">
		<div class="row-fluid">

			<div class="btn-toolbar">
				<button class="btn btn-primary" onClick="location='jobList.php'"><i class="icon-list"></i>职位列表
				</button>
				<div class="btn-group">
				</div>
			</div>

			<div class="well">
				<div id="myTabContent" class="tab-content">
					<div class="tab-pane active in" id="home">
						<form method="post" enctype="multipart/form-data">
							<label for="name">职位名称</label>
							<input type="text" name="name" id="name" class="input-xxlarge"
								   placeholder="请输入职位名称">
							<label for="depId">部门</label>
							<select name="depId" id="depId" class="input-xlarge">
								<option value="0">请选择</option>
							</select>
							<label></label>
							<input class="btn btn-primary" type="submit" value="提交"/>
						</form>
					</div>
				</div>
			</div>
			<?php include_once('common/footer.php'); ?>
		</div>
	</div>
</div>
</body>
<?php include_once('common/script.php'); ?>
<script>
    (function () {
        $.ajax({
            type: 'post',
            url: 'ajax.php',
            data: {
                action: 'dep',
                adminId: '<?php echo $loginAuth['id']; ?>'
            },
            dataType: 'json',
            success: function (res) {
                if (res) {
                    if (res.code === 1) {
                        let option = '';
                        for (let item of res.data) {
                            option += `<option value="${item.id}">${item.name}</option>`;
                        }
                        $('#depId').append(option);
                    }
                }
            }
        })
    })();
</script>
</html>

